package router

import (
	"fmt"

	"gitee.com/krio/grpc-gin-client/core/lib"

	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

const (
	baseFileName = "/apidocs.swagger.json" // swagger首页，注意：这里要和ginc里的设置成同名称
)

// 初始化swagger文档
func initSwagger(g *gin.Engine) *gin.Engine {
	// 是否开启swagger
	if lib.C.Swagger != nil && lib.C.Swagger.Enable {
		g.StaticFile(baseFileName, swaggerFilePath(lib.C.Swagger.Dir, baseFileName))
		g.GET("/api/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.URL(baseFileName)))
	}
	return g
}

// swaggerFilePath 拼接swagger文件名称
func swaggerFilePath(dir, name string) string {
	return fmt.Sprintf("%s/%s", dir, name)
}
